package com.jwthhealth_pub.old.report.view.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.jwthhealth_pub.R;

import java.math.BigDecimal;

import static com.jwthhealth_pub.common.LogUtil.makeLogTag;


public class Jwth_CircleProgressbar extends View {

    private static String TAG = makeLogTag(CircleProgressbar.class);

    private float mSweepAngle = 0;
    private String mScoreText;
    private int mCircleWidth;
    private int mCircleBg;
    private int mCircleRaidus;
    private float mTextSize;
    private int mAssistCircleWidth = 2;
    private float totalAngle = 220f;
    private float mUnityDegrees;
    private float standardBorder;

    public void setProgressbar(float mSweepAngle) {
        this.mSweepAngle = mSweepAngle;
//        mScoreText = mSweepAngle / totalAngle * 100f + "";
        //保留一位小数
        float f = mSweepAngle / totalAngle * 100f;
        BigDecimal b = new BigDecimal(f);
        float _f = b.setScale(1, BigDecimal.ROUND_HALF_UP).floatValue();//保留一位小数

        mScoreText = _f + "";
//        mScoreText = Math.round(mSweepAngle / totalAngle * 100) + "";

        invalidate();
    }

    public void setProgressbar(float mSweepAngle, float score) {
        this.mSweepAngle = mSweepAngle;
//        boolean isInteger = RegularUtil.isInteger(score + "");
//        if (isInteger) {
//            mScoreText = String.valueOf((int) score);
//            Log.d(TAG, "mScoreText:" + mScoreText);
//        } else
            mScoreText = score + "";
        invalidate();
    }

    private Paint mArcPaint;
    private Paint mTextScorePaint;
    private Paint mTextDescPaint;

    public Jwth_CircleProgressbar(Context context) {
        super(context);
        init();
    }

    public Jwth_CircleProgressbar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CircleProgress);

        mCircleWidth = ta.getDimensionPixelOffset(R.styleable.CircleProgress_circle_width, (int) standardBorder / 2);
        mCircleBg = ta.getColor(R.styleable.CircleProgress_circle_background_bg, getResources().getColor(R.color.dividual_gray));
//        mCircleBg = ta.getColor(R.styleable.CircleProgress_circle_background_bg, ContextCompat.getColor(App.mContext, R.color.dividual_gray));
        mCircleRaidus = ta.getDimensionPixelOffset(R.styleable.CircleProgress_circle_radius, 400);
        mTextSize = ta.getDimension(R.styleable.CircleProgress_score_textsize, 100);

        ta.recycle();
        init();
    }

    private void init() {
        Paint mCirclePaint = new Paint();
        mCirclePaint.setStyle(Paint.Style.STROKE);//绘制空心圆，矩形
        mCirclePaint.setAntiAlias(true);
        mCirclePaint.setColor(mCircleBg);
        mCirclePaint.setStrokeWidth(mCircleWidth);

        mArcPaint = new Paint();
        mArcPaint.setAntiAlias(true);
        mArcPaint.setStyle(Paint.Style.STROKE);
        mArcPaint.setStrokeWidth(mCircleWidth);

        mTextScorePaint = new Paint();
        mTextScorePaint.setAntiAlias(true);
        mTextScorePaint.setTextSize(mTextSize / 3);
        mTextScorePaint.setTextAlign(Paint.Align.CENTER);
        mTextScorePaint.setColor(Color.BLACK);

        mTextDescPaint = new Paint();
        mTextDescPaint.setAntiAlias(true);
        mTextDescPaint.setTextSize(mTextSize / 3);
        mTextDescPaint.setTextAlign(Paint.Align.CENTER);
        mTextDescPaint.setColor(Color.BLACK);

        Paint mAssistCirclePaint = new Paint();
        mAssistCirclePaint.setAntiAlias(true);
        mAssistCirclePaint.setStyle(Paint.Style.STROKE);
        mAssistCirclePaint.setColor(getResources().getColor(R.color.dividual_gray));
//        mAssistCirclePaint.setColor(ContextCompat.getColor(App.mContext, R.color.dividual_gray));
        mAssistCirclePaint.setStrokeWidth(mAssistCircleWidth);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        int mCencerY = getHeight() / 3 * 2;
        int mCencerX = getWidth() / 2;

//        canvas.drawCircle(mCencerX, mCencerY, mCircleRaidus, mCirclePaint);
//        canvas.drawCircle(mCencerX, mCencerY, mCircleRaidus+mCircleWidth/2+mAssistCircleWidth/2, mAssistCirclePaint);
//        canvas.drawCircle(mCencerX, mCencerY, mCircleRaidus-mCircleWidth/2-mAssistCircleWidth/2, mAssistCirclePaint);
//        canvas.drawCircle(mCencerX, mCencerY, mCircleRaidus+(mCircleWidth*3/2)+mAssistCircleWidth*3/2, mAssistCirclePaint);

//        Resources resources = getResources();

        //扇形颜色渐变，float[]参数：0f-0°, 1f-360°
//        SweepGradient sweepGradient = new SweepGradient(mCencerX, mCencerY, new int[]{resources.getColor(R.color.circleprogress_red), Color.YELLOW, resources.getColor(R.color.circleprogress_green)}, new float[]{0f, .5f, 1f});
//        SweepGradient sweepGradient = new SweepGradient(mCencerX, mCencerY, new int[]{ContextCompat.getColor(getContext(), R.color.circleprogress_red), Color.YELLOW, ContextCompat.getColor(App.mContext, R.color.circleprogress_green)}, new float[]{0f, .5f, 1f});
//        Matrix gradientMatrix = new Matrix();
//        gradientMatrix.preRotate(90, mCencerX, mCencerY);//渐变色起始位置顺时针旋转90度
//        sweepGradient.setLocalMatrix(gradientMatrix);
//        mArcPaint.setShader(sweepGradient);


        //得分圆环
        RectF mRectf = new RectF(mCencerX - mCircleRaidus, mCencerY - mCircleRaidus, mCencerX + mCircleRaidus, mCencerY + mCircleRaidus);//颜色容器
//        mArcPaint.setColor(ContextCompat.getColor(App.mContext, R.color.primarycolor));
        mArcPaint.setColor(getResources().getColor(R.color.primarycolor));
        mArcPaint.setStrokeWidth(26);
        canvas.drawArc(mRectf, 160, mSweepAngle, false, mArcPaint);

        //得分圆环外框
        canvas.save();
        RectF _score_rectf_border_outside = new RectF(mCencerX - mCircleRaidus - 13, mCencerY - mCircleRaidus - 13, mCencerX + mCircleRaidus + 13, mCencerY + mCircleRaidus + 13);
        RectF _score_rectf_border_inside = new RectF(mCencerX - mCircleRaidus + 13, mCencerY - mCircleRaidus + 13, mCencerX + mCircleRaidus - 13, mCencerY + mCircleRaidus - 13);
        mArcPaint.setColor(Color.parseColor("#c7c3c1"));
        mArcPaint.setStrokeWidth(2);
        canvas.drawArc(_score_rectf_border_outside, 160, totalAngle, false, mArcPaint);
        canvas.drawArc(_score_rectf_border_inside, 160, totalAngle, false, mArcPaint);
        //封口
        canvas.rotate(totalAngle / 2, mCencerX, mCencerY);
        canvas.drawLine(mCencerX, mCencerY - mCircleRaidus + 13, mCencerX, mCencerY - mCircleRaidus - 13, mArcPaint);
        canvas.rotate(360 - totalAngle, mCencerX, mCencerY);
        canvas.drawLine(mCencerX, mCencerY - mCircleRaidus + 13, mCencerX, mCencerY - mCircleRaidus - 13, mArcPaint);
        canvas.restore();

        //标准圆环
        standardBorder = mCircleRaidus / 5.76f + 20;
//        RectF _standard_rectf = new RectF(mCencerX - mCircleRaidus + standardBorder, mCencerY - mCircleRaidus + standardBorder, mCencerX + mCircleRaidus - standardBorder, mCencerY + mCircleRaidus - standardBorder);
        RectF _standard_rectf = new RectF(mCencerX - mCircleRaidus + standardBorder, mCencerY - mCircleRaidus + standardBorder, mCencerX + mCircleRaidus - standardBorder, mCencerY + mCircleRaidus - standardBorder);
        //根据得分绘制圆环颜色
        mArcPaint.setColor(Color.parseColor("#eeeeee"));
//        mArcPaint.setStrokeWidth(46);

        mArcPaint.setStrokeWidth(standardBorder);
        //底层灰色圆环
        canvas.drawArc(_standard_rectf, 160, totalAngle, false, mArcPaint);

        //着色得分圆环
//        mSweepAngle = 1;
//        if (mSweepAngle >= 0 && mSweepAngle < totalAngle / 5){
//            mArcPaint.setColor(getResources().getColor(R.color.circleprogress_red));
//            canvas.drawArc(_standard_rectf, 160, totalAngle/5, false, mArcPaint);
//        }
//        else if (mSweepAngle < totalAngle / 5 * 2){
//            mArcPaint.setColor(getResources().getColor(R.color.circleprogress_yellow));
//            canvas.drawArc(_standard_rectf, 160+totalAngle/5, totalAngle/5, false, mArcPaint);
//        }
//        else if (mSweepAngle < totalAngle / 5 * 3){
//            mArcPaint.setColor(getResources().getColor(R.color.circleprogress_yellow));
//            canvas.drawArc(_standard_rectf, 160+totalAngle/5*2, totalAngle/5, false, mArcPaint);
//        }
//        else if (mSweepAngle < totalAngle / 5 * 4){
//            mArcPaint.setColor(getResources().getColor(R.color.circleprogress_green));
//            canvas.drawArc(_standard_rectf, 160+totalAngle/5*3, totalAngle/5, false, mArcPaint);
//        }
//        else if (mSweepAngle < totalAngle){
//            mArcPaint.setColor(getResources().getColor(R.color.circleprogress_green));
//            canvas.drawArc(_standard_rectf, 160+totalAngle/5*4, totalAngle/5, false, mArcPaint);
//        }


        //标准圆环边框
//        RectF _standard_rectf_border_outside = new RectF(mCencerX - mCircleRaidus + 49 - standardBorder/2, mCencerY - mCircleRaidus + (standardBorder+1) - standardBorder/2, mCencerX + mCircleRaidus - 49 + standardBorder/2, mCencerY + mCircleRaidus - (standardBorder+1) + standardBorder/2);//标准圆环外边框
//        RectF _standard_rectf_border_inside = new RectF(mCencerX - mCircleRaidus + 49 + standardBorder/2, mCencerY - mCircleRaidus + (standardBorder+1) + standardBorder/2, mCencerX + mCircleRaidus - 49 - standardBorder/2, mCencerY + mCircleRaidus - (standardBorder+1) - standardBorder/2);//标准圆环内边框
        RectF _standard_rectf_border_outside = new RectF(mCencerX - mCircleRaidus + (standardBorder - 1) - standardBorder / 2, mCencerY - mCircleRaidus + (standardBorder + 1) - standardBorder / 2, mCencerX + mCircleRaidus - (standardBorder - 1) + standardBorder / 2, mCencerY + mCircleRaidus - (standardBorder + 1) + standardBorder / 2);//标准圆环外边框
        RectF _standard_rectf_border_inside = new RectF(mCencerX - mCircleRaidus + (standardBorder - 1) + standardBorder / 2, mCencerY - mCircleRaidus + (standardBorder + 1) + standardBorder / 2, mCencerX + mCircleRaidus - (standardBorder - 1) - standardBorder / 2, mCencerY + mCircleRaidus - (standardBorder + 1) - standardBorder / 2);//标准圆环内边框

        mArcPaint.setColor(Color.parseColor("#c7c3c1"));
        mArcPaint.setStrokeWidth(2);
        canvas.drawArc(_standard_rectf_border_outside, 160, totalAngle, false, mArcPaint);
        canvas.drawArc(_standard_rectf_border_inside, 160, totalAngle, false, mArcPaint);

        //刻度
        canvas.save();
        mUnityDegrees = totalAngle / 5;
        canvas.rotate(mUnityDegrees / 2, mCencerX, mCencerY);
        canvas.drawLine(mCencerX, mCencerY - mCircleRaidus + (standardBorder + 1) + standardBorder / 2, mCencerX, mCencerY - mCircleRaidus + (standardBorder + 1) - standardBorder / 2, mArcPaint);

        for (int i = 0; i < 2; i++) {
            canvas.rotate(mUnityDegrees, mCencerX, mCencerY);
            canvas.drawLine(mCencerX, mCencerY - mCircleRaidus + (standardBorder + 1) + standardBorder / 2, mCencerX, mCencerY - mCircleRaidus + (standardBorder + 1) - standardBorder / 2, mArcPaint);
        }

        canvas.rotate(360 - mUnityDegrees * 3, mCencerX, mCencerY);
        canvas.drawLine(mCencerX, mCencerY - mCircleRaidus + (standardBorder + 1) + standardBorder / 2, mCencerX, mCencerY - mCircleRaidus + (standardBorder + 1) - standardBorder / 2, mArcPaint);
        for (int i = 0; i < 2; i++) {
            canvas.rotate(360 - mUnityDegrees, mCencerX, mCencerY);
            canvas.drawLine(mCencerX, mCencerY - mCircleRaidus + (standardBorder + 1) + standardBorder / 2, mCencerX, mCencerY - mCircleRaidus + (standardBorder + 1) - standardBorder / 2, mArcPaint);
        }

        canvas.restore();

        //高风险', '低风险', '亚健康', '较健康', '很健康'
        canvas.save();
        float _textSize = mTextSize / 3;
        mTextDescPaint.setTextSize(_textSize);
        canvas.drawText("一般", mCencerX, mCencerY - mCircleRaidus + standardBorder + 12, mTextDescPaint);

//        canvas.drawText();

        canvas.rotate(mUnityDegrees, mCencerX, mCencerY);
        canvas.drawText("良好", mCencerX, mCencerY - mCircleRaidus + standardBorder + 12, mTextDescPaint);
        canvas.rotate(mUnityDegrees, mCencerX, mCencerY);
        canvas.drawText("优秀", mCencerX, mCencerY - mCircleRaidus + standardBorder + 12, mTextDescPaint);
        canvas.rotate(180, mCencerX, mCencerY);
        canvas.drawText("差", mCencerX, mCencerY - mCircleRaidus + standardBorder + 12, mTextDescPaint);
        canvas.rotate(mUnityDegrees, mCencerX, mCencerY);
        canvas.drawText("较差", mCencerX, mCencerY - mCircleRaidus + standardBorder + 12, mTextDescPaint);
        mTextDescPaint.setTextSize(mTextSize / 3);
        canvas.restore();

        //0, 20, 40, 60, 80, 100
        canvas.save();
        int _edge = 10;
        if (mTextSize == 0) {
            mTextDescPaint.setTextSize(20);
            _edge = 20;
        } else
            mTextDescPaint.setTextSize(mTextSize / 3);
        canvas.rotate(mUnityDegrees / 2, mCencerX, mCencerY);
        canvas.drawText("60", mCencerX, mCencerY - mCircleRaidus + (standardBorder + 1) + standardBorder / 2 + _textSize + _edge, mTextDescPaint);
        canvas.rotate(mUnityDegrees, mCencerX, mCencerY);
        canvas.drawText("80", mCencerX, mCencerY - mCircleRaidus + (standardBorder + 1) + standardBorder / 2 + _textSize + _edge, mTextDescPaint);
        canvas.rotate(mUnityDegrees, mCencerX, mCencerY);
        canvas.drawText("100", mCencerX, mCencerY - mCircleRaidus + (standardBorder + 1) + standardBorder / 2 + _textSize + _edge, mTextDescPaint);
        canvas.rotate(360 - mUnityDegrees * 5, mCencerX, mCencerY);
        canvas.drawText("0", mCencerX, mCencerY - mCircleRaidus + (standardBorder + 1) + standardBorder / 2 + _textSize + _edge, mTextDescPaint);
        canvas.rotate(mUnityDegrees, mCencerX, mCencerY);
        canvas.drawText("20", mCencerX, mCencerY - mCircleRaidus + (standardBorder + 1) + standardBorder / 2 + _textSize + _edge, mTextDescPaint);
        canvas.rotate(mUnityDegrees, mCencerX, mCencerY);
        canvas.drawText("40", mCencerX, mCencerY - mCircleRaidus + (standardBorder + 1) + standardBorder / 2 + _textSize + _edge, mTextDescPaint);
        canvas.restore();

        //得分
        if (mTextSize == 0)
            mTextScorePaint.setTextSize(45);
        else
            mTextScorePaint.setTextSize(mTextSize);
        Paint.FontMetricsInt fontMetrics = mTextScorePaint.getFontMetricsInt();
        int baseline = (int) ((mRectf.bottom + mRectf.top - fontMetrics.bottom - fontMetrics.top) / 2);
        if (mScoreText != null) {
            canvas.drawText(mScoreText, mRectf.centerX(), baseline - mTextSize / 4, mTextScorePaint);
        }

//        canvas.drawText("当前得分", mRectf.centerX(), baseline + mTextSize / 5, mTextDescPaint);
    }
}
